<cfcomponent name="CrafterDateSeries" extends="crafter.CrafterDocument" accessors="yes" output="no" hint="">
	
	
	<!--- Properties --->
	<cfproperty name="hasValidDate" type="boolean" />
	<cfproperty name="nextDate" type="date" />
	
	
	<!--- load() --->
	<cffunction name="load" access="public" returntype="any" output="no">
		<cfargument name="path" type="string" required="yes" />
		<cfscript>
			
			// Do the standard CrafterDocument load()
			super.load(
				path = arguments.path
			);
			
			// Search and return the active date
			local.dteActiveDate = getActiveDate();
			
			// Look to see if the active date year return is 1900. This is
			// our flag for no valid dates.
			if (Year(local.dteActiveDate) EQ 1900) {
				// This is a false date.
				setHasValidDate(false);
			}
			else {
				// Note the date as being valid.
				setHasValidDate(true);
			}
			
			// Whether or not the date is valid, we're going to go ahead and set it
			// to the next date.  Application logic should probably avoid showing this
			// date (use getHasValidDate() to check), but if all else fails the next
			// date method needs to return a date of some kind.
			setNextDate(local.dteActiveDate);
			return this;
			
			
		</cfscript>
	</cffunction>
	
	
	<!--- getActiveDate() --->
	<cffunction name="getActiveDate" access="public" returntype="date" output="no">
		
		<cfscript>
			
			local.dteToday = Now();
			local.dteFallBackDate = CreateDate(1900,1,1);
			local.sFallBackMask = "mmmm d";
			local.nSmallestDateDiff = 1000;
			local.nIndexOfSmallestDateDiff = 0;
			
			// Search the xml for all start dates
			local.arStartDates = XmlSearch(getXml(),"//startDate");
			
			// Grab the masks
			local.arMasks = XmlSearch(getXml(),"//outputMask");
			
			// Look to see if there are any results
			if (ArrayLen(local.arStartDates) GT 0) {
				
				/* 
					We found start dates.  So, no what we want to do is loop over them
					and compare the difference between the current date and the start date
					to try and find the small difference without going over.
				*/
				
				for (local.i=1; local.i LTE ArrayLen(local.arStartDates); local.i++) {
					
					local.dteCurrentStartDate = ParseDateTime(local.arStartDates[local.i].XmlText);
					local.nCurrentDateDiff = DateDiff("d",local.dteCurrentStartDate,local.dteToday);
					
					// First, the date diff should be a positive number. As we have it set up that means
					// that that date has passed.
					if (local.nCurrentDateDiff GTE 0) {
						// Now, let's see if it's the smallest difference we've found yet.
						if (local.nCurrentDateDiff LT local.nSmallestDateDiff) {
							// This is the smallest difference found yet. Note the diff and index.
							local.nSmallestDateDiff = 	local.nCurrentDateDiff;
							local.nIndexOfSmallestDateDiff = local.i;
						}
					}
					
				}
				
				// We should now have the index of the most recent item. We'll pull out the actual display dates
				// and return the one with the match index
				if (local.nIndexOfSmallestDateDiff GT 0) {
					
					local.arDisplayDates = XmlSearch(getXml(),"//displayDate");
					local.dteSelectedDate = ParseDateTime(local.arDisplayDates[local.nIndexOfSmallestDateDiff].XmlText);
					local.sSelectedMask = local.arMasks[local.nIndexOfSmallestDateDiff].XmlText;
					return DateFormat(local.dteSelectedDate,local.sSelectedMask);
					
				}
				else {
					return DateFormat(local.dteFallBackDate, local.sFallBackMask);
				}

				
			}
			else {
				// No start dates were found.	
				return DateFormat(local.dteFallBackDate, local.sFallBackMask);
			}
		
		</cfscript>
	</cffunction>
	
	
</cfcomponent>